<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/3/27
 * Time: 17:54
 * 积分操作
 */
namespace app\home\controller;
use app\models\Bases;
use think\Config;

class Integral extends Base{
    /**
     * @return \think\response\Json
     * 减去用户的积分
     */
    public function decIntegral(){
        try{
            $uid = checkLogin();
            if(empty($uid)) throw new \LogicException('请先登录',10030);
            $user = new Bases('user');
            $userInfo  = self::getUserInfo("uid = $uid");//获取用户信息
            //判断该用户是否扣除积分了
            $deduction_integral_time = strtotime(date('Y-m-d',time())); //今天的开始时间
            if($userInfo['deduction_integral_time'] == $deduction_integral_time){
                throw new \LogicException('已经扣除出积分了',10032);
            }
            if($userInfo['integral'] < Config::get('integral.reading'))
                throw new \LogicException('积分不够',10031);
            if($userInfo['type'] == 3){ //只有普通会员才扣积分
                if(!$user->_setDec("uid = $uid","integral",Config::get('integral.reading'))){//扣除该用户的积分
                    throw new \LogicException('操作失败',10010);
                }
                if(!$user->setField("uid = $uid",['deduction_integral_time'=>$deduction_integral_time])){ //更新扣除积分时间
                    throw new \LogicException('操作失败',10031);
                }
            }
            $integral = self::getUserInfo("uid = $uid","integral");
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult(['integral'=>$integral]));
    }

    /**
     * @return string
     * 主要验证今日的积分是否扣除  is_deduction_integral 扣除积分  is_not_deduction_integral 积分已经扣除扣除
     */
    public function checkDeductionIntegral(){
        $uid =intval(checkLogin());
        $userInfo  = self::getUserInfo("uid = $uid");       //获取用户信息
        $deduction_integral_time = strtotime(date('Y-m-d',time())); //今天的开始时间\
        if($userInfo['deduction_integral_time'] != $deduction_integral_time){
            return 'is_deduction_integral';
        }
        return 'is_not_deduction_integral';
    }

    /**
     * @return \think\response\Json
     * 用户签到添加积分
     */
    public function incIntegral(){
        try{
            $uid = checkLogin();
            if(empty($uid)) throw new \LogicException('请先登录',10030);
            $user = new Bases('user');
            $userInfo  = self::getUserInfo("uid = $uid");
            if($userInfo['type']==3){
                $check_in_time = strtotime(date('Y-m-d',time())); //今天的开始时间
                if($check_in_time  ==  $userInfo['check_in_time']){
                    throw new \LogicException('今天已经签到了！',10010); //判断该用户今天是否签到了
                }
                if(!$user->setField("uid = $uid",['check_in_time'=>$check_in_time])){
                    throw new \LogicException('签到失败！',10010);
                }
                if(!$user->_setInc("uid = $uid","integral",Config::get('integral.check_in'))){
                    throw new \LogicException('操作失败！',10010);
                }
            }
            $integral = self::getUserInfo("uid = $uid","integral");
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult(['integral'=>$integral]));
    }

    /**
     * @return \think\response\Json
     * 用户阅读文章
     */
    public function readShare(){
        try{
            $integral = new Integral();
            $infomation = new Information();
            $uid = checkLogin();
            if(empty($uid)) throw new \LogicException('请先登录',10030);
            $share_user = new Bases('shares_user');
            $user = new Bases('user');
            $post = input('param.');
            $post['create_time'] = time();
            $post['uid'] = $uid;
            $info = $share_user->find("shares_id = {$post['shares_id']} and uid = $uid");
            if(!empty($info)){
                throw new \LogicException('你已经付过积分了',10029);//验证用户是否有使用积分阅读过
            }
            $userInfo  = self::getUserInfo("uid = $uid");           //获取用户信息
            $reading_integral = $infomation->deductIntegral();
            if($userInfo['integral'] < $reading_integral)
                throw new \LogicException('积分不够',10031);       //验证积分是否足够
            if($userInfo['type'] == 3){ //只有普通会员才扣积分
                //获取该用户本次应该扣除多少积分
                if(!$user->_setDec("uid = $uid","integral",$reading_integral)){  //扣除该用户的积分
                    throw new \LogicException('操作失败',10010);
                }
                if(!$share_user->insertData($post)){
                    throw new \LogicException('操作失败',10010);
                }
            }
            $integral = self::getUserInfo("uid = $uid","integral");
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult(['integral'=>$integral]));
    }

    /**
     * @return \think\response\Json
     * 是否有牛股推荐接口
     */
    public function isShare(){
        try{
            $shares = new Bases('shares');
            $start_time = strtotime(date('Y-m-d')); //开始时间
            $end_time = $start_time + 3600 * 24 ;
            $shares_info = $shares->selectData("recommend_time >= $start_time and recommend_time <= $end_time");
            if(empty($shares_info)){
                throw new \LogicException('今日没有任何推荐信息',10058);
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult());
    }

}